package net.entelijan.cobean.text.validator;

import net.entelijan.cobean.text.IValidationResult;
import net.entelijan.cobean.text.IValidator;

public class NumberRangeValidator<T extends Number> implements IValidator<T> {

	private Number min;

	private Number max;

	public NumberRangeValidator() {
		super();
	}

	public NumberRangeValidator(Number min, Number max) {
		super();
		this.min = min;
		this.max = max;
	}

	@Override
	public IValidationResult validate(final Number value) {
		if (value == null || value.doubleValue() < min.doubleValue()
				|| value.doubleValue() > max.doubleValue()) {
			return new IValidationResult() {

				@Override
				public String getMessage() {
					return (value == null ? "''" : value)
							+ " is not between " + min + " and " + max;
				}

				@Override
				public boolean ok() {
					return false;
				}

			};

		} else {
			return new IValidationResult() {

				@Override
				public String getMessage() {
					return null;
				}

				@Override
				public boolean ok() {
					return true;
				}

			};
		}
	}

	public Number getMin() {
		return min;
	}

	public void setMin(Number min) {
		this.min = min;
	}

	public Number getMax() {
		return max;
	}

	public void setMax(Number max) {
		this.max = max;
	}

}
